package com.credithc.eagleeye.api.advice;
import com.credithc.eagleeye.api.config.ApplicationConfig;
import com.credithc.eagleeye.api.exception.AuthTokenException;
import com.credithc.eagleeye.api.vo.PageVo;
import com.credithc.eagleeye.api.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * 描 述: <br/>
 * 作 者: zhangjin<br/>
 * 创 建：2018年02月09日<br/>
 * 版 本：v1.0.0<br>
 * <p>
 * 历 史: (版本) 作者 时间 注释 <br/>
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {
    @Autowired
    private ApplicationConfig applicationConfig;

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ResultVo exceptionHandler(HttpServletRequest request, HttpServletResponse response, Exception ex) {
        ResultVo resultVo = new ResultVo();
        resultVo.setCode("9999");
        resultVo.setMsg(ex.getMessage());
        resultVo.setPage(new PageVo());
        return resultVo;
    }
    @ExceptionHandler(value = AuthTokenException.class)
    @ResponseBody
    public ResultVo exceptionAuthTokenHandler(HttpServletRequest request, HttpServletResponse response, Exception ex) {
        ResultVo resultVo = new ResultVo();
        resultVo.setCode("9001");
        resultVo.setMsg("token无效");
        resultVo.setPage(new PageVo());
        return resultVo;
    }
}
